import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Confirm from '#cpt/temp/Confirm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $http, $Lazy, globalConfig } = window
const { COVIDUrl } = globalConfig
// ===================================================================== Lazy
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const $time 			= $Lazy.file(()=>import('@com/utils/time'))
// ===================================================================== data
const AuditStatus = [{ name:'全部', value:-99 },{ name:'待审核', value:0 },{ name:'已审核', value:1 },{ name:'已作废', value:-1 }]
const TestStatus = [{ name:'待检', value:0 },{ name:'已检', value:1 },{ name:'迟发', value:2 },{ name:'退单', value:-1 },{ name:'终止', value:-2 }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '标本条码',			name: 'spec_code' },
			{ label: '实验号',			name: 'experiment_num' },
			{ label: '板号',				name: 'board_no' },
			{ label: '医院',				name: 'hosp_id',			type: 'select', loading:true,	idStr:'uuid',	nameStr:'hospName' },
			{ label: '标本状态',			name: 'status',				type: 'select', 	data:AuditStatus },
			{ label: '检测状态',			name: 'test_state',			type: 'select', 	data:TestStatus },
			{ label: '接收时间',			names: ['check_time_start','check_time_end'], type:'date-range', name:'jz-time' },
		],
	}
	
	model = {}
	defaultTime = {}
	permissions = {status:1,download:1 }
	
	componentDidMount(){
		const { ban_btn } = $fn.local('permissions')
		if($fn.hasArray(ban_btn)){
			if(ban_btn.includes('codeinfo.status')){		// 作废
				this.permissions.status = 0
			}
			if(ban_btn.includes('codeinfo.download')){		// 导出
				this.permissions.download = 0
			}
		}
		this.getSelect()
	}
	onTime = reset => {
		 $time.then(f=>{
			const arr = f.duration['2-9-now']()
			this.resetModel={check_time_start:arr[0],check_time_end:arr[1],'jz-time':arr}
			this.model = $fn.copy(this.resetModel)
			this.fetch()
		})
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 医院
			this.hostData = await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000,status:1} } )
			$fn.setRow(forms, 'hosp_id', this.hostData )
			this.setState({ forms })
			this.onTime()
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,COVIDUrl+'codeinfo/waitQuery',{COVID:true})
	
	// 表单
	cols = [
		{ title: '标本条码',			dataIndex: 'specCode', 			width:130 },
		{ title: '实验号',			dataIndex: 'experimentNum', 	width:100 },
		{ title: '板号', 			dataIndex: 'boardNo', 			width:100 },
		{ title: '所属医院', 		dataIndex: 'hospId', 			width:200,				render: text => $fn.getState(this.hostData,text,'hospName','uuid') },
		{ title: '标本状态', 		dataIndex: 'status', 			width:100,				render: text => $fn.getState(AuditStatus,text) },
		{ title: '检测状态', 		dataIndex: 'testState', 		width:100,				render: text => $fn.getState(TestStatus,text) },
		{ title: '接收时间', 		dataIndex: 'checkTime', 		...$fn.table.time },
		{ title: '备注', 			dataIndex: 'remark', 			width:200 },
	]
	
	// 操作按钮
	controls = () => {
		const { selectedKeys } = this.state
		const uuids = selectedKeys.join(',')
		const {status,download } = this.permissions
		return [
			status && Confirm.call(this, '批量作废', COVIDUrl+'codeinfo/status', { isKeys:1, query:{status:-1,uuids} }),
			download && Controls.click.call(this,'导出', ()=>{
				const param = {...this.model, had_info:0}
				$http.submit(null, COVIDUrl+'codeinfo/download', { param }).then((data)=>{
					if(data){
						window.open(`${COVIDUrl}download?rand=${data}`)
					}
				})
			},{ghost:true,code:'F1'})
		]
	}
	
	render(){
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this,{ timeStr:'2-9-now' }) }
				{/* 表格 */}
				{ Table.call(this) }
			</>
		)
	}
}

export default Index